﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;

namespace OA.BLL.DB_help
{

    public sealed class NHibernateHelper
    {

 
        private static Dictionary<string, ISessionFactory> sessionFactorys = new Dictionary<string, ISessionFactory>();

        static NHibernateHelper()
        {
         //   sessionFactory = new Configuration().Configure().BuildSessionFactory();
        }


        public static void ClearAllState()
        {
            HttpContext context = HttpContext.Current;
            foreach (var db in sessionFactorys.Keys)
            {
                ISession currentSession = context.Items[Common.Keys.NhibernateCurrentSessionKey + db] as ISession;
                if (currentSession != null)
                {
                    currentSession.Clear();
                }
            }
        }

        public static ISession GetCurrentSession(string db)
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[Common.Keys.NhibernateCurrentSessionKey + db] as ISession;
            if (currentSession == null)
            {
                ISessionFactory sessionFactory;
                lock (sessionFactorys)
                {
                    if (sessionFactorys.ContainsKey(db))
                    {
                        sessionFactory = sessionFactorys[db];
                    }
                    else
                    {
                        var xmlpath = context.Server.MapPath("~/Content/DB_Config/" + db + ".xml");
                        sessionFactory = new Configuration().Configure(xmlpath).BuildSessionFactory();
                        sessionFactorys.Add(db, sessionFactory);
                    }
                }
                currentSession = sessionFactory.OpenSession(new ShowSql());
                context.Items[Common.Keys.NhibernateCurrentSessionKey + db] = currentSession;
            }
            
            return currentSession;
        }

        public static void CloseSession(string db)
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[Common.Keys.NhibernateCurrentSessionKey + db] as ISession;
            if (currentSession != null)
            {
                currentSession.Close();
                context.Items[Common.Keys.NhibernateCurrentSessionKey + db] = null;
            }
        }

        public static void CloseAllSession()
        {
            HttpContext context = HttpContext.Current;
            foreach (var db in sessionFactorys.Keys)
            {
                CloseSession(db);
            }
        }

        public static void CloseSessionFactory(string db)
        {
            if (sessionFactorys.ContainsKey(db))
            {
                sessionFactorys[db].Close();
            }
        }

        public static void CloseAllSessionFactory()
        {
            foreach (string i in sessionFactorys.Keys)
            {
                sessionFactorys[i].Close();
                sessionFactorys.Remove(i);
            }
        }

    }
}
